import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MovieDaoImpl implements MovieDao {

    @Override
    public boolean addMovie(Movie movie) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO movie VALUES (?, ?, ?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, movie.getMovieId());
            pstmt.setString(2, movie.getName());
            pstmt.setString(3, movie.getDirector());
            pstmt.setString(4, movie.getActor());
            pstmt.setString(5, movie.getIntro());
            pstmt.setInt(6, movie.getDuration());
            pstmt.setDouble(7, movie.getScore());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeResources(conn, pstmt, null);
        }
    }

    @Override
    public List<Movie> queryMovies(String name, String director, String actor) {
        List<Movie> movies = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            StringBuilder sql = new StringBuilder("SELECT * FROM movie WHERE 1=1");
            List<Object> params = new ArrayList<>();

            if (name != null && !name.isEmpty()) {
                sql.append(" AND name LIKE ?");
                params.add("%" + name + "%");
            }
            if (director != null && !director.isEmpty()) {
                sql.append(" AND director LIKE ?");
                params.add("%" + director + "%");
            }
            if (actor != null && !actor.isEmpty()) {
                sql.append(" AND actor LIKE ?");
                params.add("%" + actor + "%");
            }

            pstmt = conn.prepareStatement(sql.toString());
            for (int i = 0; i < params.size(); i++) {
                pstmt.setObject(i + 1, params.get(i));
            }
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Movie movie = new Movie(
                    rs.getString("movie_id"),
                    rs.getString("name"),
                    rs.getString("director"),
                    rs.getString("actor"),
                    rs.getString("intro"),
                    rs.getInt("duration"),
                    rs.getDouble("score")
                );
                movies.add(movie);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(conn, pstmt, rs);
        }
        return movies;
    }

    @Override
    public List<Movie> getAllMovies() {
        return queryMovies(null, null, null);
    }

    @Override
    public Movie getMovieById(String movieId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM movie WHERE movie_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, movieId);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                return new Movie(
                    rs.getString("movie_id"),
                    rs.getString("name"),
                    rs.getString("director"),
                    rs.getString("actor"),
                    rs.getString("intro"),
                    rs.getInt("duration"),
                    rs.getDouble("score")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(conn, pstmt, rs);
        }
        return null;
    }
}
    